<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="../css/picker.css" />
		<link rel="stylesheet" href="../css/poppicker.css" />
		<link rel="stylesheet" type="text/css" href="../css/style.css" />
		<link rel="stylesheet" href="../css/member.css" />
	</head>

	<body>
		<header class="mui-bar mui-bar-nav public-color">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">添加新地址</h1>
			<button class="mui-pull-right edit" id="btn" style="background-color: #ff7831;border: 0px;color: #efecec; font-size: 16px;">保存</button>
		</header>
		<div class="mui-content address_content">
			<form class="mui-input-group">
				<div class="mui-input-row ">
					<label>收货人</label>
					<input type="text" class="mui-input-group" placeholder="请输入收货人用户名">
				</div>
				<div class="mui-input-row">
					<label>联系电话</label>
					<input type="text" class="mui-input-group" placeholder="请输入手机号码">
				</div>
				<div class="mui-input-row">
					<label>所在地区</label>
					<input type="text" id="district" readonly="readonly" class="mui-input-group" placeholder="请选择地区">
				</div>
				<div class="mui-input-row">
					<label>详细地址</label>
					<input type="text" class="mui-input-group" placeholder="请输入详细地址">
				</div>
			</form>
			<div class="address_mor clearfix">
				<span>设为默认</span>
				<div id="isdefault" class="mui-switch mui-switch-mini" data-mystate="1">
					<div class="mui-switch-handle"></div>
				</div>
			</div>
		</div>
		<input type="hidden" id='order_ids'>
		<script type="text/javascript" src="../js/jquery.min.js"></script>
		<script src="../js/mui.min.js"></script>
		<script src="../js/castapp.js"></script>
		<script src="../js/picker.js"></script>
		<script src="../js/poppicker.js"></script>
		<script src="../js/city.data-3.js"></script>
		<script src="../js/app.js"></script>
		<script>
			ca.init();
			$("#order_ids").val(getUrlParam("order_ids"));
			var btn = ca.id('btn');
			var input = ca.tagName('input');
			var dis = ca.id('district');
			dis.addEventListener('tap', function() {
				//三级组合
				castapp.combinationSelect(3, cityData3, function(arr) {
					dis.value = arr;
				});
			})
			btn.addEventListener('tap', function() {
				var username = input['0'].value;
				var tel = input['1'].value;
				var district = input['2'].value;
				var address = input['3'].value;
				if(username == "") {
					ca.prompt('收货人姓名不能为空');
					return;
				}
				if(tel == "") {
					ca.prompt('手机号码不能为空');
					return;
				}
				if(district == "") {
					ca.prompt('地区不能为空');
					return;
				}
				if(address == "") {
					ca.prompt('详细地址不能为空');
					return;
				}
				btn.disabled = true;
				var disarr = district.split(',');
				//默认状态
				var isdefault = $("#isdefault").data("mystate");
				//写入数据库
				var member = JSON.parse(localStorage.getItem('member'));
				$.ajax({
					//type: 'post',
					url: APP.api_url + 'Member/address_add',
					data: {
						token: member['token'],
						uid: getUid(),
						realname: username,
						mobile: tel,
						province: disarr[0],
						city: disarr[1],
						district: disarr[2],
						address: address,
						isdefault: isdefault
					},
					success: function(data) {
						if(data.code == 200) {

							if($("#order_ids").val().length > 1) {
								location.href = 'order_address.html?order_ids=' + getUrlParam("order_ids");
							} else {
								location.href = 'address.html';
							}
						} else {
							console.log(data.msg);
						}
					}
				});
			});
		</script>
		<script>
			$("#isdefault").click(function() {
				var my = $("#isdefault").data("mystate");
				var my_state = 1;
				if(parseInt(my) == 1) {
					$("#isdefault").data("mystate", 2);
				} else {
					$("#isdefault").data("mystate", 1);
				}
				//alert($("#isdefault").data("mystate"));
			})
		</script>